package com.library_manage_system.strategy;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.security.auth.login.Configuration;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.library_manage_system.button.ClarityButton;
import com.library_manage_system.button.ConfirmButton;
import com.library_manage_system.button.SummitButton;
import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.Table;

public class StaticsPanelStrategy implements PanelStrategy {

	private CardLayout cardLayout;
	private JPanel staticsPanel;
	protected String item;
	protected String text;
	public StaticsPanelStrategy(){
		
		cardLayout = new CardLayout();
		// 新建一个主面板并设置相关参数
		staticsPanel = new JPanel();
		staticsPanel.setLayout(cardLayout);
		staticsPanel.setBackground(Color.gray);
		
		//初始化item和text;
		item = "";
		text = "";
	}
	
	@Override
	public JPanel createPanel(ArrayList<Book> arraylist,WindowsMainImpl windowsMainImpl) {
		// TODO Auto-generated method stub

		//新建面板
		JPanel staticsByWhat = staticsByWhat(arraylist);
		staticsPanel.add(staticsByWhat,"staticsByWhat");
		cardLayout.show(staticsPanel, "staticsByWhat");
		
		return staticsPanel;
	}

	// 新建第一个显示的面板，用于提示用户根据什么来统计
	public JPanel staticsByWhat(final ArrayList<Book> arraylist) {

		JPanel jpanel1 = new JPanel();
		JPanel jpanel2 = new JPanel();
		
		JLabel jLabel = new JLabel("请问你想根据什么来统计");

		// 设置一个下拉框用来选择客户想要什么来统计
		String[] s = { "主编", "出版社", "出版年" };
		final JComboBox jComboBox = new JComboBox(s);

		// 设置一个按钮确定以这个来统计
		SummitButton summitButton = new SummitButton();
	    ConfirmButton confirmButton = new ConfirmButton();
		ClarityButton clarityButton = new ClarityButton(confirmButton);
		JButton jButton = clarityButton.beautify();

		jpanel1.setLayout(new GridLayout(3, 1,50, 50));
		jpanel1.add(jLabel);
		jpanel1.add(jComboBox);
		jpanel1.add(jButton);
		
		jpanel2.add(jpanel1);
		
		//为这个按钮设置点击事件，点击即调到第二个界面
		jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				item = (String) jComboBox.getSelectedItem();
				System.out.println(item);
				JPanel loggingin = loggingin(arraylist);
				staticsPanel.add(loggingin,"loggingin");
				
				cardLayout.show(staticsPanel, "loggingin");
			}
		});

		return jpanel2;
	}

	// 新建一个输入面板
	public JPanel loggingin(final ArrayList<Book> arraylist) {

		JPanel jPanel1 = new JPanel();
		JPanel jPanel2 = new JPanel();

		// 设置中间面板为用户输入的信息
		JLabel jl1 = new JLabel("请输入你想统计的主编或者出版社，或者书名");
		final JTextField jt = new JTextField(20);
		
		//设置一个按钮确定跳至下一个界面
		JButton jbutton = new JButton("确定");
		
		jPanel1.setLayout(new GridLayout(3, 1,50, 50));
		jPanel1.add(jl1);
		jPanel1.add(jt);
		jPanel1.add(jbutton);
		
		jPanel2.add(jPanel1);
		
		//为这个界面的按钮设置点击事件,点击即调到第三个界面
		jbutton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//获得输入的内容
				text = jt.getText();
				JPanel staticsTablePanel1 = staticsTablePanel1(arraylist);
				staticsPanel.add(staticsTablePanel1,"staticsTablePanel1");
				
				cardLayout.show(staticsPanel, "staticsTablePanel1");
			}
		});
		
		return jPanel2;
	}

	// 新建一个统计面板
	public JPanel staticsTablePanel1(ArrayList<Book> arraylist) {

		JPanel jtablePanel = new JPanel();
		jtablePanel.setLayout(new BorderLayout());
		
		ArrayList<Book> list = statics(arraylist);
		
		//创建一个表格
		JTable jtable = Table.createTable(list);
		Table.makeFace(jtable);
		
		JPanel jbuttonPanel = new JPanel();
		JButton jButton = new JButton("返回");
		jbuttonPanel.add(jButton);
		
		jtablePanel.add(jtable.getTableHeader(), BorderLayout.NORTH);
		jtablePanel.add(jtable, BorderLayout.CENTER);
		jtablePanel.add(jbuttonPanel, BorderLayout.SOUTH);
		
		//为按钮设置点击事件返回第一个界面
		jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				cardLayout.show(staticsPanel, "staticsByWhat");
				
			}
		});
		
		return jtablePanel;
	}
	
	public ArrayList<Book> statics(ArrayList<Book> arraylist){
		
		ArrayList<Book> list = new ArrayList<Book>();
		
		//解析item是根据什么来筛选
		int value = 0;
		if(item.trim().equals("主编")){
			value = 1;
		}else if(item.equals("出版社")){
			value = 2;
		}else if(item.equals("出版年")){
			value = 3;
		}
		switch (value) {
		case 1:
			Iterator<Book> it = arraylist.iterator();
			while(it.hasNext()){
				Book book = it.next();
				if(book.getAuthor().equals(text)){
					list.add(book);
				}
				
			}
			break;
		case 2:
			Iterator<Book> it1 = arraylist.iterator();
			while(it1.hasNext()){
				Book book = it1.next();
				if(book.getPress().equals(text)){
					list.add(book);
				}
			}
			break;
		case 3:
			Iterator<Book> it2 = arraylist.iterator();
			while(it2.hasNext()){
				Book book = it2.next();
				if(book.getPress_year().equals(text)){
					list.add(book);
				}
			}
			break;
		default :
			
			break;
		}
		return list;
	}

}
